package sprint.doing.thread;

import java.io.BufferedReader;
import java.util.ArrayList;

import sprint.model.FriendModel;
import sprint.model.LoginModel;
import sprint.model.Protocal;
import sprint.model.TrModel;
import sprint.view.MineView;

public class InStream extends Thread {
	BufferedReader br;
	public int check = 0;
	public String serchReshult[] ;
	MineView mv;
	ArrayList<TrModel> transMassge ;
	ArrayList<LoginModel> transLogin ;
	ArrayList<FriendModel> friendList ;
	
	public InStream(BufferedReader br,ArrayList<FriendModel> friendList, MineView mv){
		this.br=br;
		this.friendList = friendList;
		this.mv = mv;
	}
	
	public void run(){
		System.out.println("inputstream 시작....");
		while(true){
			try{
				System.out.println("tr 대기중");
				String tran = br.readLine();
				System.out.println("서버 Tr : "+tran);
				String [] tr = tran.split(":");
				int protocal =Integer.parseInt(tr[1]);
				if(protocal == Protocal.joinSucces){
					
					
				}else if(protocal == Protocal.friend){
					for(int i=1; i<=tr.length;i++){
						if(i % 3 == 0){
							FriendModel fm = new FriendModel();
							fm.setFriendName(tr[i]);
							fm.setFriendState(Integer.parseInt(tr[i+1].trim()));
							friendList.add(fm);
						}
					}
				}else if(protocal == Protocal.sendMassge){
					TrModel tm = new TrModel();
					tm.setId(tr[1]);
					tm.setMessage(tr[2]);
					tm.setTargetId(tr[3]);
					mv.talk.showMassge.append(tm.getId()+" : "+tm.getMessage()+"\n");
			
				}else if(protocal == Protocal.joinfalse){
					
				}else if(protocal == Protocal.loginfalse){
					check = 2;
				}else if(protocal == Protocal.loginSucces){
					check =1;
					if(tr.length > 3){
						for(int i=1; i<=tr.length;i++){
							if(i % 3 == 0){
								FriendModel fm = new FriendModel();
								fm.setFriendName(tr[i]);
								fm.setFriendState(Integer.parseInt(tr[i+1].trim()));
								friendList.add(fm);
							}
						}
					}
					
				}else if(protocal == Protocal.serchResult){		
					System.out.println("결과 탐");
					System.out.println("tr : "+tr[2]);
					if(!tr[2].equals("false")){
						serchReshult = new String[tr.length-2];
						System.out.println("tr 길이 "+tr.length);
						for(int i=0; i<= tr.length; i++){
							System.out.println(""+i+"tr 내용 "+tr[i]);
							if(i > 3){
								serchReshult[i-4] = tr[i];
							}
							System.out.println(""+i+" 결과 내용 : "+serchReshult[i]);
						}
					}
				}

			}catch(Exception e){}
		}
	}
}
